using System;
using System.Collections.Generic;
using Admin3000.Backend.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using Admin3000.Backend.Api.ParamModel;
using Newtonsoft.Json;
using Admin3000.Backend.Api.Repository;
using Admin3000.Backend.Api.Utils;

namespace Admin3000.Backend.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UsersController : ControllerBase
    {
        private IRepository<Users> _usersRespository;

        public UsersController(IRepository<Users> usersRespository)
        {
            _usersRespository=usersRespository;
        }

        [HttpGet]
        public dynamic Get([FromQuery] Pager pager)
        {
            //get请求默认从uri从获取参数，所以此处应该加上FromQuery的特性才能正确获得参数
            var pageIndex = pager.pageIndex;
            var pageSize = pager.pageSize;

            // 获得所有满足条件的记录
            var users = _usersRespository.Table.Where(x => x.IsDeleted == false);

            // 对获得的记录分页
            var u = users.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            // 返回的数据里，带有数据和分页信息
            var res = new
            {
                Code = 1000,
                Data = new { Data = u, Pager = new { pageIndex, pageSize, rowsTotal = users.Count() } },
                Msg = "获取用户列表成功^_^"
            };
            return res;
        }

        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var user = _usersRespository.GetById(id);
            return user;
        }

        [HttpPost]
        public string Post(NewUser model)
        {
            JsonSerializerSettings settings = new JsonSerializerSettings();
            // settings.MissingMemberHandling = MissingMemberHandling.Ignore;
            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

            var username = model.Username.Trim();
            var password = model.Password.Trim();
            var remarks = model.Remarks.Trim();

            if (string.IsNullOrEmpty(username))
            {
                var tmp = new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "用户名不能为空，请确认后重试"
                };
                return JsonHelper.Serialize(tmp);
            }

            var user = new Users
            {
                Username = username,
                Password = password,
                Remarks = remarks
            };

            _usersRespository.Insert(user);

            var res = new
            {
                Code = 1000,
                Data = user,
                Msg = "创建用户成功"
            };
            return JsonHelper.Serialize(res);
        }

        [HttpPut("{id}")]
        public dynamic Put(int id, NewUser model)
        {
            var user = _usersRespository.GetById(id);

            if (user != null)
            {
                user.Username = model.Username;
                user.Remarks = model.Remarks;
                user.UpdatedTime = DateTime.Now;
                _usersRespository.Update(user);
                return JsonHelper.Serialize(new
                {
                    Code = 1000,
                    Data = user,
                    Msg = string.Format("你修改的用户的id为：{0}，已经修改成功，请注意查收", id)
                });
            }
            else
            {
                return JsonHelper.Serialize(new
                {
                    Code = 104,
                    Data = "",
                    Msg = "指定Id的用户不存在，请确认后重试"
                });
            }
        }

        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            var user = _usersRespository.GetById(id);
            if (user != null)
            {
                _usersRespository.Delete(id);
                return new
                {
                    Code = 1000,
                    Msg = string.Format("删除指定id为{0}的用户成功", id)
                };
            }
            else
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "指定Id的用户不存在，请确认后重试"
                };
            }

        }

        private IEnumerable<Users> GetUsers()
        {
            var users =
                new List<Users> {
                    new Users {
                        Id = 1,
                        Username = "admin",
                        Password = "芝麻开门"
                    },
                    new Users {
                        Id = 2,
                        Username = "超级管理员",
                        Password = "芝麻开门"
                    },
                    new Users {
                        Id = 3,
                        Username = "普通用户",
                        Password = "芝麻开门"
                    }
                };

            return users;
        }
    }
}
